<!DOCTYPE html>
<html>
<!--
Copyright 2014 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Performance Tests - goog.structs.Queue</title>
  <link rel="stylesheet" type="text/css" href="../testing/performancetable.css"/>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.functions');
    goog.require('goog.structs.Queue');
    goog.require('goog.testing.PerformanceTable');
    goog.require('goog.testing.jsunit');
  </script>
</head>
<body>
  <h1>goog.structs.Queue Performance Tests</h1>
  <p>
    <strong>User-agent:</strong>
    <script>document.write(navigator.userAgent);</script>
  </p>
  <div id="perfTable"></div>
  <hr>

  <script>
    var table = new goog.testing.PerformanceTable(
        goog.dom.getElement('perfTable'));

    // Number of operations to measure in each table line.
    var OPS_COUNT = 10000;

    var smallSizes = [1, 2, 5, 10, 20, 50, 100, 1000, 10000];
    var largeSizes = [100000];

    function populateQueue(size) {
      var q = new goog.structs.Queue();
      for (var i = 0; i < size; ++i) {
        q.enqueue(i);
      }
      return q;
    }

    function enqueueDequeueTest(size) {
      var q = populateQueue(size);
      table.run(function() {
        for (var i = 0; i < OPS_COUNT; ++i) {
          q.dequeue();
          q.enqueue(i);
        }
      }, 'Enqueue and dequeue, size ' + size);
    }

    function containsTest(size) {
      var q = populateQueue(size);
      table.run(function() {
        for (var i = 0; i < OPS_COUNT; ++i) {
          q.contains(i);
        }
      }, 'Contains every element, size ' + size);
    }

    function removeTest(size) {
      var q = populateQueue(size);
      if (size == 1) {
        return;
      }
      table.run(function() {
        var offset = Math.round(size / 2);
        for (var i = 0; i < OPS_COUNT; ++i) {
          q.remove(i + offset);
          q.enqueue(i + size);
        }
      }, 'Remove element from the middle, size ' + size);
    }

    function getValuesTest(size) {
      var q = populateQueue(size);
      table.run(function() {
        for (var i = 0; i < OPS_COUNT; ++i) {
          q.getValues()[size - 1];
        }
      }, 'Get values, size ' + size);
    }

    function testEnqueueDequeueSmall() {
      goog.array.forEach(smallSizes, enqueueDequeueTest);
    }

    function testEnqueueDequeueLarge() {
      goog.array.forEach(largeSizes, enqueueDequeueTest);
    }

    function testContainsSmall() {
      goog.array.forEach(smallSizes, containsTest);
    }

    function testContainsLarge() {
      goog.array.forEach(largeSizes, containsTest);
    }

    function testRemoveSmall() {
      goog.array.forEach(smallSizes, removeTest);
    }

    function testRemoveLarge() {
      goog.array.forEach(largeSizes, removeTest);
    }

    function testGetValuesSmall() {
      goog.array.forEach(smallSizes, getValuesTest);
    }

    function testGetValuesLarge() {
      goog.array.forEach(largeSizes, getValuesTest);
    }

  </script>
</body>
</html>
